package fr.cephb.joperon.core.iterator;

import java.util.Iterator;

import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

import fr.cephb.util.Pair;

/**
 * An iterator returning the <b>KEY</b> component of a delegate
 * iterator Iterator<Pair<DatabaseEntry, DatabaseEntry>>
 * It is used to extract the <b>value</b> encoded in the key DatabaseEntry from a delegate iterator and
 * 
 * @param <T> the value encoded in the key DatabaseEntry
 */
public abstract class DatabaseKeyIterator<T>
	extends DelegateIterator<Pair<DatabaseEntry, DatabaseEntry>,T>
	{
	/**
	 * Constructir
	 * @param delegate the delegate iterator where we should extract the value of the key
	 */
	public DatabaseKeyIterator
		(
		Iterator<Pair<DatabaseEntry, DatabaseEntry>> delegate) {
		super(delegate);
		}
	
	
	@Override
	protected final T extractValue(Pair<DatabaseEntry, DatabaseEntry> input) {
		try
			{
			return extractFromKey(input.first(),input.second());
			}
		catch(DatabaseException err)
			{
			close();
			throw new RuntimeException(err);
			}
		}
	/** returns the value from a pair(key,value) of the delegate Iterator */
	protected abstract T extractFromKey(DatabaseEntry  key,DatabaseEntry  value) throws DatabaseException;
	
	}
